//Global variables
var map;
var realms =[];

function onLoad(){
createMap();
//getRealms();
}

function execRealmUpdater(){
	
	document.getElementById('post_realm_loc').value=document.getElementById('encodedPolyline').value;
	document.getElementById('post_realm_level').value=document.getElementById('encodedLevels').value;
	
	//document.getElementById('post_form').submit();
}

$(document).ready(refreshRealmSelect);	


function refreshRealmSelect(){
	$.getJSON("http://swankestlabs.com/smallsmallworld/google_maps/get_realms.php",
	function(data){	
	for(var i=0;i<data.realm.length;i++)	
		realms.push(data.realm[i]);
	populateRealmDropDown(data.realm);
	});
}
	
function populateRealmDropDown( realm ){
	var realm_select = $("#realm_select").get(0);
	for(var i=0;i<realms.length; i++){
	var tmp_option = document.createElement('option');
	tmp_option.text = realm[i].name;
	tmp_option.id="realm_option_" +i;
	try {
   	 realm_select.add(tmp_option, null); // standards compliant; doesn't work in IE
  	}
  	catch(ex) {
 	 realm_select.add(tmp_option); // IE only
 	 }
}//end for loop
}//end function

function displayRealm(){
	var index = document.getElementById('realm_select').selectedIndex;
	//alert("Realm " + realms[index].name + " was selected.");
	map.clearOverlays();
	drawPolygon(realms[index].loc,realms[index].level);
}//end function

function deleteRealm(){
	$.getJSON("realm_updater.php",
	{ "function":"deleteRealm", "realm_id":realms[document.getElementById('realm_select').selectedIndex].id },
	function(data){
	document.getElementById("small_world_display").innerText = data.result;
	});
}	

function drawPolygon(encoded_points,encoded_levels){	  
var polygon = new GPolygon.fromEncoded({
	  polylines: [
	    {points: encoded_points,
	     levels: encoded_levels,
	     color: "#f33f00",
	     opacity: 0.7,
	     weight: 3,
	     numLevels: 18,
	     zoomFactor: 2}],
	  fill: true,
	  color: "#f33f00",
	  opacity: 0.4,
	  outline: true
	});

	map.addOverlay(polygon);
}



	
	

	

	



